<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/01/016
 * Time: 下午4:58
 */

namespace Admin\Controller;
use Think\Controller;

class BillboardController extends Controller {


    /*
     * 检查是否登录
     */
    public function checklogin (){

        //$this->isAjaxLogin();

    }

    /*
     * 列表
     */
    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $page = I('post.page/d');
        $page_size = I('post.page_size/d');
       

        $ArticleModel = M('billboard_game');

        $count = $ArticleModel->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;

        $result = $ArticleModel->order('rank desc')->limit($limitStart,$page_size)->select();

        if($result !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $result;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*保存*/
    public function save(){

        $this->isAjaxLogin();//检查是否登录

        $ArticleModel = M('game_advertisers');

        $id     = I('post.id/d');
        $game_id   = I('post.game_id');
        $game_name=$ArticleModel->field("game_name")->where("id=".$game_id)->find();
        $name=implode(',', $game_name);

        
      

        $data['game_id']   = $game_id;
        $data['game_name']   = $name;

        

        
        /*修改*/
        if($id){

            $model=M('billboard_game');

            $result = $model->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

        }
        /*添加*/
        else{


            $model=M('billboard_game');
            $arr=$model->select();


            if($arr==null){
                $data['rank']=1;
            }else{
                foreach($arr as $v){
                    $data['rank'] = $v['id']+1;
                }   
            }
            
           

            $lastid = $model->add($data);

            if($lastid !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据添加失败，请稍后在试！";
            }
        }

        $this->ajaxReturn($ajaxReturn);

    }

    /*
     * 详情
     */
    public function infos (){

        $this->isAjaxLogin();

        $id = I('post.id/d');

        $ArticleModel = M('game_advertisers');

        $result = $ArticleModel->field("id,game_name")->select();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*
     * 删除
     */
    public function delete (){

        $ids = I('post.id');

        $Article = M('billboard_game');


        $ids = explode(',',$ids);

        foreach($ids as $id){
            $Article-> where('id='.$id)->delete();
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }



    /*
     * 审核
     */
    public function review (){

        $ids = I('post.id');

        $type = I('post.type');

        $Article = M('Article');

        $ids = explode(',',$ids);

        foreach($ids as $id){
            $Article-> where('id='.$id)->setField('status',$type);
        }

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg'] = "SUCCESS";


        $this->ajaxReturn($ajaxReturn);

    }


    /*
     * 排序
     */
    public function rank (){

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('billboard_game');

        $thisinfo   = $ArticleModel->where('id='.$thisid)->getField('rank,game_name');
        $thisrank   = array_keys($thisinfo)[0];
        $thiscid    = array_values($thisinfo)[0];


        $nextinfo   = $ArticleModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('id,rank');
        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        if($thisrank && $nextid && $nextrank){

            $ArticleModel-> where('id='.$thisid)->setField('rank',$nextrank);
            $ArticleModel-> where('id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }


}